package com.credit.jumu.playcard.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.credit.jumu.playcard.BuildConfig;
import com.credit.jumu.playcard.R;
import com.credit.jumu.playcard.base.BaseActivity;
import com.credit.jumu.playcard.cardapp.CardApp;
import com.credit.jumu.playcard.utils.DataSharedPreference;
import com.credit.jumu.playcard.utils.JsonUtils;
import com.credit.jumu.playcard.utils.LogUtils;
import com.credit.jumu.playcard.utils.MD5Encoder;
import com.credit.jumu.playcard.utils.SignUtil;
import com.credit.jumu.playcard.utils.SystemUtil;
import com.credit.jumu.playcard.utils.ToastUtil;
import com.credit.jumu.playcard.utils.TypeUtils;
import com.credit.jumu.playcard.utils.UrlsUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;

/**
 * Created by Administrator on 2017/10/21/021.
 */
//注册
public class RegisterActivity extends BaseActivity implements View.OnClickListener {
	TextView getcode;
	private DataSharedPreference ds;
	EditText et_phonenum, et_sendcode, et_mypass, et_incode;
	TextView tosign;

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
		ds = new DataSharedPreference(this);
	}

	public void initView() {
		setContentView(R.layout.activity_register);
		getcode = (TextView) findViewById(R.id.getcode);
		getcode.setOnClickListener(this);
		et_phonenum = (EditText) findViewById(R.id.et_phonenum);
		et_sendcode = (EditText) findViewById(R.id.et_sendcode);
		et_mypass = (EditText) findViewById(R.id.et_mypass);
		et_incode = (EditText) findViewById(R.id.et_incode);
		tosign = (TextView) findViewById(R.id.tosign);
		tosign.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.getcode:
				if (TextUtils.isEmpty(et_phonenum.getText().toString())) {
					ToastUtil.showShortToast("请输入手机号码");
					return;
				}
				getCode(et_phonenum.getText().toString());
				break;
			case R.id.tosign:
				if (TextUtils.isEmpty(et_phonenum.getText().toString())) {
					ToastUtil.showShortToast("请输入手机号码");
				} else if (TextUtils.isEmpty(et_sendcode.getText().toString())) {
					ToastUtil.showShortToast("请输入验证码");
				} else if (TextUtils.isEmpty(et_mypass.getText().toString())) {
					ToastUtil.showShortToast("请输入密码");
					break;
				} else {
					gotoRegister(et_phonenum.getText().toString(), et_mypass.getText().toString(),
							et_sendcode.getText().toString(), et_incode.getText().toString());
				}
		}
	}

	public void gotoRegister(String num, String pass, String code, String encode) {
		LinkedHashMap<String, String> mapSign = new LinkedHashMap<>();
		mapSign.put("actionType", "register");
		mapSign.put("deviceCode", ds.getUUID());
		mapSign.put("intefaceType", CardApp.intefaceType);
		mapSign.put("mobile", num);
		mapSign.put("passWord", MD5Encoder.encodeBase64(pass));
		mapSign.put("smsCode", code);
		mapSign.put("source",CardApp.source);
		mapSign.put("terminalId", "A");
		mapSign.put("timeStamp", SystemUtil.getSystemTime());
		mapSign.put("version", BuildConfig.VERSION_NAME);
		mapSign.put("key", CardApp.key);

		LinkedHashMap<String, String> mapPramas = new LinkedHashMap<>();
		mapPramas.put("accountId", "");
		mapPramas.put("actionType", "register");
		mapPramas.put("deviceCode", ds.getUUID());
		mapPramas.put("intefaceType", CardApp.intefaceType);
		mapPramas.put("mobile", num);
		mapPramas.put("passWord", MD5Encoder.encodeBase64(pass));
		mapPramas.put("sign", MD5Encoder.encode(SignUtil.getSign(mapSign)));
		mapPramas.put("smsCode", code);
		mapPramas.put("source", CardApp.source);
		mapPramas.put("terminalId", "A");
		mapPramas.put("timeStamp", SystemUtil.getSystemTime());
		mapPramas.put("token", "");
		mapPramas.put("version", BuildConfig.VERSION_NAME);

		OkGo.<String>post(UrlsUtils.REGISTER)
				.params(mapPramas)
				.execute(
						new StringCallback() {
							@Override
							public void onSuccess(Response<String> response) {


								JSONObject responseJson = JsonUtils.getJsonObject(response.body().toString());
								String code = TypeUtils.getJsonString(responseJson, "code", "");
								String message = TypeUtils.getJsonString(responseJson, "message", "");
								ToastUtil.showShortToast(message);
								LogUtils.println("zhangyiyionSuccessRegister------" + response.body());
								RegisterActivity.this.finish();
							}

							@Override
							public void onError(Response<String> response) {
								LogUtils.println("zhangyiyionErrRegister------" + response.body() + response.message() + response.code());
							}
						}
				);


	}

	//获取验证码
	public void getCode(String num) {
		LinkedHashMap<String, String> mapSign = new LinkedHashMap<>();
		mapSign.put("deviceCode", ds.getUUID());
		mapSign.put("intefaceType", CardApp.intefaceType);
		mapSign.put("mobile", num);
		mapSign.put("source", CardApp.source);
		mapSign.put("terminalId", "A");
		mapSign.put("timeStamp", SystemUtil.getSystemTime());
		mapSign.put("version", BuildConfig.VERSION_NAME);
		mapSign.put("key", CardApp.key);

		LinkedHashMap<String, String> mapPramas = new LinkedHashMap<>();
		mapPramas.put("accountId", "");
		mapPramas.put("deviceCode", ds.getUUID());
		mapPramas.put("intefaceType", CardApp.intefaceType);
		mapPramas.put("mobile", num);
		mapPramas.put("sign", MD5Encoder.encode(SignUtil.getSign(mapSign)));
		mapPramas.put("source", CardApp.source);
		mapPramas.put("terminalId", "A");
		mapPramas.put("timeStamp", SystemUtil.getSystemTime());
		mapPramas.put("token", "");
		mapPramas.put("version", BuildConfig.VERSION_NAME);
		OkGo.<String>post(UrlsUtils.SEND_CODE)
				.params(mapPramas)
				.execute(
						new StringCallback() {
							@Override
							public void onSuccess(Response<String> response) {
								if (!TextUtils.isEmpty(response.body())) {
									JSONObject responseJson = JsonUtils.getJsonObject(response.body().toString());
									String code = TypeUtils.getJsonString(responseJson, "code", "");
									String message = TypeUtils.getJsonString(responseJson, "message", "");
//									if(!TextUtils.isEmpty(code)&&"9999".equals(code)){
									ToastUtil.showShortToast(message);
//									}
								}
							}

							@Override
							public void onError(Response<String> response) {
								LogUtils.println("zhangyiyionGetMessageCodeerr------" + response.body() + response.message() + response.code());
							}
						}
				);
	}
}
